<?php
/*
 * Handler for multiselect using checkboxes
 */

m2import('phpinchains/psp/Tag.php');

class h_multiSelectHandler extends phpinchains_psp_Tag
{

    /**
     * Is a body tag (if false, body is ignored)
     */
    var $tagType = PSP_EMPTY_TAG;
    /**
     * Tag name in form namespace:tagname
     */
    var $tagName   = 'h:multiSelect';
    var $requiredAttributes = array(
        'name' => '[\w\d_\-]+',
        'value' => '*',
        'optiontext' => '[\w\d_\-]+',
        'optionvalue' => '[\w\d_\-]+'
    );
    var $attributes = array(
        'validate', // TODO implement validation here
        'validationParam',
        'columns',
        'containerStyle',
        'optionStyle',
        'columnStyle',
        'containerClass',
        'optionClass',
        'columnClass',
        'selected' // array of values: array(1,2,3,4,5,...) or array(array($optionvalue => 1),array($optionvalue => 2),...)
    );

    function doStartTag()
    {
        $name = $this->getAttribute('name');
        $optiontext  = $this->getAttribute('optiontext');
        $optionvalue = $this->getAttribute('optionvalue');
        $selected = $this->getAttribute('selected');
        $columns = (int) $this->getAttribute('columns');
        if (is_array($selected[0])) {
            $selected_tmp = array();
            foreach ($selected as $sel) {
                $selected_tmp[] = $sel[$optionvalue];
            }

            $selected = $selected_tmp;
        }
        $this->out->write('<div class="'.$this->getAttribute('containerClass').'" style="'.$this->getAttribute('containerStyle').'">');
        $value = $this->getAttribute('value');
        $colmnClassStyle =  ' class="'.$this->getAttribute('columnClass').'" style="float:left;'.$this->getAttribute('columnStyle').'"';
        $this->out->write('<div'.$colmnClassStyle.'>');
        $valcount = count($value);
        $perCol   = ceil($valcount/$columns);
        for ($i = 0; $i < $valcount; $i++) {
            $selstr = (in_array($value[$i][$optionvalue], $selected))?' checked="checked"':'';
            $this->out->write('<input type="checkbox"'.$selstr.' name="'.$name.'[]" id="'.$name.'_'.$value[$i][$optionvalue].'" value="'.$value[$i][$optionvalue].'"/>'.
                    '<label for="'.$name.'_'.$value[$i][$optionvalue].'">'.$value[$i][$optiontext].'</label><br/>');
            if ($columns && ($i+1)%$perCol == 0) {
                $this->out->write('</div>'."\n".(($i+1<$valcount)?'<div'.$colmnClassStyle.'>':''));
            }
        }
        $this->out->write('</div><div style="clear:both"></div></div>');
        return PSP_SKIP_BODY;
    }

    // In empty tag only necessary, if tag skips page, e.g. redirect -> no further processing needed
    function doEndTag()
    {
        //return PSP_SKIP_PAGE;
    }

    // TODO: implement validation
    function startElement (phpinchains_parser_psp_PSPParser $sender, $attrs, $line, $file = '')
    {
        $form = $sender->topParent();
        if (!isset($form['type']) || $form['type'] != 'form')
        {
            die('h:'.$this->tagname.' must not be used within other tag than h:form (except flow control tags)!');
        }
        if (!empty($attrs['validate']) && (empty($form['name']) || empty($attrs['name'])))
        {
            die("If h:'.$this->tagname.' tag has attribute validate than it also needs attribute name and the surrounding form tag must have attribute name, too!");
        }
        if (!empty($form['name']) && empty($attrs['validate']))
        {
            $sender->addFormField(substr($form['name'], 1, -1), substr($attrs['name'], 1, -1));
        }
        parent::startElement($sender, $attrs, $line, $file);
    }

}
